package com.greenleaf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenleaf.entity.SearchCon;
import com.greenleaf.entity.User;
import com.greenleaf.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;

	@RequestMapping("/get")
	@ResponseBody
//http://localhost:8080/Greenleaf/user/get
	public Object getAllUser(SearchCon searchCon) {
		List<User> user = userService.getAllUser(searchCon);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", userService.getTotalUser(searchCon));
		map.put("data", user);
		return map;
	}
	@RequestMapping("/add")
	@ResponseBody
//http://localhost:8080/Greenleaf/user/add
	public Object addUser(User user) {
		int r = userService.addUser(user);
		return r==1?true:false;
	}
	@RequestMapping("/update")
	@ResponseBody
	//http://localhost:8080/Greenleaf/user/update
	public Object updateUser(User user) {
		int r = userService.updateUser(user);
		return r==1?true:false;
	}
	@RequestMapping("/del")
	@ResponseBody
	//http://localhost:8080/Greenleaf/user/del
	public Object delUser(Integer userId) {
		int r = userService.delUser(userId);
		return r==1?true:false;
	}
	@RequestMapping("/dels")
	@ResponseBody
	//http://localhost:8080/Greenleaf/user/dels
	public Object delProducts(String ids) {
		System.out.println(ids);
		System.out.println(ids);
		System.out.println(ids);
		System.out.println(ids);
		String[] listIds = ids.split(",");
		List<Integer> list = new ArrayList<Integer>();
		for (String listStr : listIds) {
			list.add(Integer.parseInt(listStr));
		}
		int r = userService.delUsers(list);
		return r!=0?true:false;
	}
}
